package com.liuyang.controller;

import cn.hutool.crypto.SecureUtil;
import com.liuyang.common.Result;
import com.liuyang.common.TableResult;
import com.liuyang.entity.Account;
import com.liuyang.entity.Logs;
import com.liuyang.service.AccountService;
import com.liuyang.service.LogsService;
import com.liuyang.service.MaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/manage")
public class SystemManageController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private MaterialsService materialsService;

    @Autowired
    private LogsService logsService;

    // 账号管理
    @PostMapping("/queryAccountManage")
    public TableResult queryAccountManage(String accountName, int page, int limit) {
        Map<String, Object> map = accountService.queryAccountManage(accountName, page, limit);
        long count = (long) map.get("count");
        List<Account> list = (List<Account>) map.get("list");
        TableResult tableResult = new TableResult(0, "", Integer.parseInt(""+count), list);
        return tableResult;
    }

    // 重置账号密码, 默认6个1
    @PostMapping("/editResetAccountPassword")
    public Result editResetAccountPassword(String id) {
        Account account = new Account();
        account.setId(id);
        account.setAccountPassword(SecureUtil.md5("111111"));
        accountService.saveOrUpdate(account);
        return Result.succ("重置密码成功");
    }

    // 禁用或开启账号
    @PostMapping("/stopAccount")
    public Result stopAccount(Account account) {
        String status = "1".equals(account.getAccountStatus())? "0": "1";
        String msg = "1".equals(status)? "账号已开启" : "账号已禁用";
        account.setAccountStatus(status);
        accountService.saveOrUpdate(account);
        return Result.succ(msg);
    }

    // 受灾数据统计 - 饼图
    @PostMapping("/queryDisasterType")
    public Result queryDisasterType() {
        List<Map> list = materialsService.queryDisasterType();// 受灾类型数据统计
        return Result.succ(list);
    }

    // 需求数据统计 - 柱状图
    @PostMapping("/queryDemandType")
    public Result queryDemandType() {
        List<Map> list = materialsService.queryDemandType(); // 需求类型数据统计
        return Result.succ(list);
    }

    @PostMapping("/queryLogsInfo")
    public TableResult queryLogsInfo(String startTime, String endTime, int page, int limit) {
        Map<String, Object> map = logsService.queryLogsInfo(startTime, endTime, page, limit);
        Long count = (Long) map.get("count");
        List<Logs> list = (List<Logs>) map.get("list");
        TableResult<Logs> tableResult = new TableResult(0, "", count.intValue(), list);
        return tableResult;
    }

}
